import { Component, Input, OnInit, ViewChild, ElementRef, ChangeDetectorRef } from '@angular/core';
import { Platform } from '@ionic/angular';
import { AlertController, ModalController, LoadingController } from '@ionic/angular';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { MapBaiduService } from 'src/app/shared/service/map.baiduapi';
import { PageModalService } from 'src/app/shared/service/page-modal-service';
import { HttpService } from '../../shared/service/http.service';
@Component({
  selector: 'app-custom-price',
  templateUrl: './custom-price.component.html',
  styleUrls: ['./custom-price.component.scss'],
})
export class CustomPriceComponent implements OnInit {
  @Input() tagData: any;

  switchButtonValue = true; // 先付费
  postSwitchButtonValue = true; // 后付费

  firstCharge = true; // 1.6.35 新增属性

  topLeftSwitchBtnId = 'topLeftSwitchBtn';
  topLeftSwitchBtnId2 = 'topLeftSwitchBtn2';

  constructor(
    private httpService: HttpService,
    private appGlobalService: AppGlobalService,
    private pageModalService: PageModalService,
    private alertController: AlertController
  ) { }

  ngOnInit() {
    this.topLeftSwitchBtnId = this.topLeftSwitchBtnId + new Date().getTime();
    // 1.6.22
    if (this.tagData.data
      && this.tagData.data.topLeftBtn
      && this.tagData.data.topLeftBtn.submit
      && this.tagData.data.topLeftBtn.submit.content && this.tagData.data.submitKeyName) {
      this.switchButtonValue =
        this.tagData.data.topLeftBtn.submit.content[this.tagData.data.submitKeyName] === '1' ? false : true;
    } else if (this.tagData.data
      && this.tagData.data.topLeftSwitchBtn
      && this.tagData.data.topLeftSwitchBtn.submit
      && this.tagData.data.topLeftSwitchBtn.submit.content && this.tagData.data.submitKeyName) {// 1.6.35
      this.switchButtonValue =
        this.tagData.data.topLeftSwitchBtn.submit.content[this.tagData.data.submitKeyName] === '1' ? false : true;
    } else if (this.tagData.data
      && this.tagData.data.topLeftPostSwitchBtn
      && this.tagData.data.topLeftPostSwitchBtn.submit
      && this.tagData.data.topLeftPostSwitchBtn.submit.content && this.tagData.data.postSubmitKeyName) {// 1.6.35
      this.postSwitchButtonValue =
        this.tagData.data.topLeftPostSwitchBtn.submit.content[this.tagData.data.postSubmitKeyName] === '1' ? false : true;
    }




    if (this.tagData.data.showType !== '1') {
      setTimeout(() => {
        this.switchChargeService();
      }, 200);
    }
  }

  demoData() {

    this.tagData = {
      type: "tag",
      style: "tag.customPrice",
      name: "app-custom-price",
      cssStyle: "XXXX",
      linearLayout: "body",
      data: {
        title: "张老板您好？",
        titleCssStyle: "font-size: 1rem;font-weight: bold;color: #0F0F0F;",
        titlePrice: "￥2900",
        titlePriceCssStyle: "XXX",
        secondLabel: "您可以自定义报价，按洽谈价格收取费用",
        secondLabelCssStyle: "XXXX",
        thirdLabel: "您可以自定义报价，按洽谈价格收取费用",
        thirdLabelCssStyle: "XXXX",
        labelIcon: "https:",
        price: "￥2300",
        originalPrice: "￥2900",
        priceCssStyle: "XXX",
        originalPriceCssStyle: "XXX",
        bgIcon: "https:",
        submitButton: {
          label: "自定义报价",
          buttonCssStyle: "XXXX",
          link: {
            type: "modal",
            params: {

            },
            target: "123"
          }
        },
        infoButton: {
          icon: "XXXX",
          buttonCssStyle: "XXXX",
          link: {
            type: "modal",
            params: {

            },
            target: "123"
          }
        },
        pricesItem: {
          label: "当前自定义报价",
          delButton: {
            label: "删除",
            buttonCssStyle: "XXXX",
            submit: {
              id: "64a743a9-7739-4338-b125-c7d2d5141290",
              content: {

              }
            }
          },
          items: [
            {
              label: "报价",
              value: "2303元",
              valueCssStyle: "XXX"
            },
            {
              label: "时间",
              value: "2022-04-09 16:00:00",
              valueCssStyle: "XXX"
            }
          ]
        }
      }
    };

  }

  goPage(item, btn?) {
    if (btn !== undefined) {
      const params = {
        content: btn.submit.content,
        // tslint:disable-next-line:no-string-literal
        id: btn.submit.id,
        sessionId: localStorage.getItem('sessionId')
      };
      // 请求获取新数据
      this.httpService.post('application/submit', params, respo => {
        // 请求数据成功
        if (respo.success) {
          if (item.link.params && item.link.params.eventReturnParam && respo.data.eventResult) {
            item.link.params[item.link.params.eventReturnParam] = respo.data.eventResult;
            this.pageModalService.openPage(item.link);
          }
          // this.appGlobalService.reloadCurrentPage();
          // this.appGlobalService.showToast(respo.message, 2000, 'middle');
        } else {
          // 请求数据失败
          this.appGlobalService.showToast(respo.message, 2000, 'middle');
        }
      },
        err => {
          // 请求数据失败
          this.appGlobalService.showToast(err.message, 2000, 'middle');
        });
    } else {

      if (item.link.type === 'ljcontract') {
        const targetObj = { pageId: item.link.target, params: item.link.params, sessionId: localStorage.getItem('sessionId') };
        this.httpService.post('element/ljserviceContract/url', targetObj.params, res => {
          const title = item.label;
          this.appGlobalService.openUrl(title, res.url, item.link.target, JSON.stringify(item.link.params));
        });
      } else {
        this.pageModalService.openPage(item.link);
      }
    }
    console.log(item);
    // this.pageModalService.openPage(item.link);
  }


  doAction(btn) {

    if (btn) {

      const params = {
        content: btn.submit.content,
        // tslint:disable-next-line:no-string-literal
        id: btn.submit.id,
        sessionId: localStorage.getItem('sessionId')
      };
      // 请求获取新数据
      this.httpService.post('application/submit', params, respo => {
        // 请求数据成功
        if (respo.success) {
          this.appGlobalService.reloadCurrentPage();
          this.appGlobalService.showToast(respo.message, 2000, 'middle');
        } else {
          // 请求数据失败
          this.appGlobalService.showToast(respo.message, 2000, 'middle');
        }
      },
        err => {
          // 请求数据失败
          this.appGlobalService.showToast(err.message, 2000, 'middle');
        });
    }

  }

  goToSharePage(shareBtn, type?) {
    this.appGlobalService.openUrl2(this.tagData.data.sharePageTitle, this.tagData.data.sharePageUrl, shareBtn);
  }

  // type27
  switchButtonSystem(type) {
    let commonSubmit;
    if (type === 0 && this.tagData.data.topLeftBtn && this.tagData.data.topLeftBtn.submit) {
      commonSubmit = this.tagData.data.topLeftBtn.submit;
      if (commonSubmit.content && this.tagData.data.submitKeyName) {
        commonSubmit.content[this.tagData.data.submitKeyName] = this.switchButtonValue === true ? 0 : 1;
      }
    } else if (type === 1 && this.tagData.data.topLeftSwitchBtn && this.tagData.data.topLeftSwitchBtn.submit) {
      commonSubmit = this.tagData.data.topLeftSwitchBtn.submit;
      if (commonSubmit.content && this.tagData.data.submitKeyName) {
        commonSubmit.content[this.tagData.data.submitKeyName] = this.switchButtonValue === true ? 0 : 1;
      }
    } else if (type === 2 && this.tagData.data.topLeftPostSwitchBtn && this.tagData.data.topLeftPostSwitchBtn.submit) {
      commonSubmit = this.tagData.data.topLeftPostSwitchBtn.submit;
      if (commonSubmit.content && this.tagData.data.postSubmitKeyName) {
        commonSubmit.content[this.tagData.data.postSubmitKeyName] = this.postSwitchButtonValue === true ? 0 : 1;
      }
    }

    const params = {
      content: commonSubmit.content,
      id: commonSubmit.id,
      sessionId: localStorage.getItem('sessionId')
    };
    // 请求获取新数据
    this.httpService.post('application/submit', params, res => {
      // 请求数据成功
      if (res.success) {
      } else {
        // 请求数据失败
        this.appGlobalService.showToast(res.message, 2000, 'middle');
      }
    },
      err => {
        // 请求数据失败
        this.appGlobalService.showToast(err.message, 2000, 'middle');
      });
  }

  goToPage(link) {
    if (link) {
      this.pageModalService.openPage(link);
    }
  }


  // 切换先收费后收费
  switchChargeService() {
    this.firstCharge = !this.firstCharge;
    const topLeftSwitchBtnEle = document.getElementById(this.topLeftSwitchBtnId) as HTMLElement;
    const topLeftSwitchBtnEle2 = document.getElementById(this.topLeftSwitchBtnId2) as HTMLElement;
    if (this.firstCharge === false) {
      if (topLeftSwitchBtnEle) {
        topLeftSwitchBtnEle.style.display = 'none';
      }
      if (topLeftSwitchBtnEle2) {
        topLeftSwitchBtnEle2.style.position = 'unset';
        topLeftSwitchBtnEle2.style.display = 'flex';
        topLeftSwitchBtnEle2.style.zIndex = '1';
      }

    } else {
      if (topLeftSwitchBtnEle) {
        topLeftSwitchBtnEle.style.display = 'flex';
      }
      if (topLeftSwitchBtnEle2) {
        topLeftSwitchBtnEle2.style.position = 'absolute';
        topLeftSwitchBtnEle2.style.display = 'none';
        topLeftSwitchBtnEle2.style.zIndex = '0';
      }
      
    }
  }


}
